﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using EnvDTE;
using System.Reflection;
using System.Diagnostics;

namespace TeamReview
{
    public static class Instrumentation
    {

        private static EnvDTE80.DTE2 _shellDTE;
        private static EnvDTE.AddIn _addInInstance;
        private static string _logFileName;
        private static string _version = "n/a";
        private static OutputWindowPane _outputWindowPane;
        private static string _exceptionAlert;

        /// <summary>
        /// Write a message to file TeamReview.Log which is where the assembly is located.
        /// Exceptions arising will be written to the file. Only catch exceptions at the highest level.
        /// </summary>
        /// <param name="message"></param>
        public static void Log(Exception e)
        {
            using (TextWriter streamWriter = new StreamWriter(_logFileName, true))
            {
                streamWriter.WriteLine(string.Format("{0} {1} {2}", DateTime.Now.Date.ToShortDateString(), DateTime.Now.ToLongTimeString(), e.ToString()));
            }
            Trace(_exceptionAlert);
        }

        public static void Configure(EnvDTE80.DTE2 shellDTE, EnvDTE.AddIn addInInstance)
        {
            _shellDTE = shellDTE;
            _addInInstance = addInInstance;

            //Log file will be in the path where the addin is installed
            Assembly asm = Assembly.GetExecutingAssembly();


            FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(asm.Location);
            FileInfo fi = new FileInfo(asm.Location);

            _version = fvi.FileVersion;
            _logFileName = string.Concat(fi.DirectoryName, @"\TeamReview.log");
            _exceptionAlert = String.Format("An exception has been encountered. {0}Please file a bug or contact a TeamReview member at http://TeamReview.codeplex.com with the contents of {1}", Environment.NewLine, _logFileName);

            SetOutputWindowPane();

           
        }

        private static void SetOutputWindowPane()
        {
            // Obtain the VS output window and write to it with OutputString
            OutputWindow outputWindow =
                (OutputWindow)_shellDTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput).Object;


            foreach (OutputWindowPane pane in outputWindow.OutputWindowPanes)
            {
                if (pane.Name == "TeamReview")
                {
                    _outputWindowPane = pane;
                    break;
                }
            }

            if (_outputWindowPane == null)
            {
                _outputWindowPane = outputWindow.OutputWindowPanes.Add("TeamReview");
                Trace(string.Format("TeamReview: {0}", _version));
                Trace("Visit: http://TeamReview.codeplex.com");
            }
        }



        public static void Trace(string message)
        {
            _outputWindowPane.OutputString(message);
            _outputWindowPane.OutputString(Environment.NewLine);
        }
    }
}
